﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DreamSpace.Net
{
    public class ThreadSafePropertyBag : IPropertyBag
    {
        Dictionary<string, object> _Properties = new Dictionary<string, object>();

        public void Add(string key, object value)
        {
            lock (_Properties)
            {
                _Properties.Add(key, value);
            }
        }

        public bool ContainsKey(string key)
        {
            lock (_Properties)
            {
                return _Properties.ContainsKey(key);
            }
        }

        public ICollection<string> Keys
        {
            get
            {
                lock (_Properties)
                {
                    return _Properties.Keys;
                }
            }
        }

        public bool Remove(string key)
        {
            lock (_Properties)
            {
                return _Properties.Remove(key);
            }
        }

        public bool TryGetValue(string key, out object value)
        {
            lock (_Properties)
            {
                return _Properties.TryGetValue(key, out value);
            }
        }

        public ICollection<object> Values
        {
            get
            {
                lock (_Properties)
                {
                    return _Properties.Values;
                }
            }
        }

        public object this[string key]
        {
            get
            {
                lock (_Properties)
                {
                    return _Properties[key];
                }
            }
            set
            {
                lock (_Properties)
                {
                    _Properties[key] = value;
                }
            }
        }

        public void Add(KeyValuePair<string, object> item)
        {
            lock (_Properties)
            {
                _Properties.Add(item.Key, item.Value);
            }
        }

        public void Clear()
        {
            lock (_Properties)
            {
                _Properties.Clear();
            }
        }

        public bool Contains(KeyValuePair<string, object> item)
        {
            lock (_Properties)
            {
                return _Properties.Contains(item);
            }
        }

        public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
        {
            lock (_Properties)
            {
                ((ICollection<KeyValuePair<string, object>>)_Properties).CopyTo(array, arrayIndex);
            }
        }

        public int Count
        {
            get
            {
                lock (_Properties)
                {
                    return _Properties.Count;
                }
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePair<string, object> item)
        {
            lock (_Properties)
            {
                return ((ICollection<KeyValuePair<string, object>>)_Properties).Remove(item);
            }
        }

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            lock (_Properties)
            {
                return _Properties.GetEnumerator();
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            lock (_Properties)
            {
                return _Properties.GetEnumerator();
            }
        }
    }
}
